package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Role;
import com.qf.enums.EmployerExceptionEnum;
import com.qf.exception.EmployerException;
import com.qf.mapper.RoleMapper;
import com.qf.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Override
    public List<Role> findDeptById(Integer deptId) {
        return roleMapper.findByDeptId(deptId);
    }

    @Override
    public PageInfo<Role> findAllInfo(Integer pageNum, Integer size, String roleName, String deptName, Integer roleStatus) {
        PageHelper.startPage(pageNum,size);
        List<Role> roleList = roleMapper.findAllInfo(roleName,deptName,roleStatus);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return pageInfo;
    }

    @Override
    @Transactional
    public void add(Role role, int[] mid) {
        //添加用户的角色信息
       Integer count = roleMapper.insertByRole(role);
       if(count!=1){
           log.error("【职位管理添加】添加角色信息失败,role={}",role);
           throw new EmployerException(EmployerExceptionEnum.ROLE_ADD_ERROR);
       }
        //添加角色
        Map map=new HashMap();
        map.put("roleId",role.getRoleId());
        map.put("mid",mid);
        int i = roleMapper.insertMiddle(map);
        if(i!=mid.length){
            log.error("【职位管理添加】添加中间表失败,ids={}", Arrays.toString(mid));
            throw new EmployerException(EmployerExceptionEnum.MENUMID_ADD_ERROR);
        }
    }
}
